<template>
  <div class="fillcontain" style="padding: 25px 15px 15px;">
    <div style="display:flex;">
      <asstnt-stndrd-items @queryStandItemTableData="queryStandItemTableData" ref="projectLibrary">
      </asstnt-stndrd-items>
      <div class="right_box">
        <div class="right-title">AI话术管理</div>
        <!-- AI话术管理列表 -->
        <div style="margin-left:40px ; margin-top: 25px;">
          <!-- 新增按钮 -->
          <el-button type="danger" @click="standPropertySaveClick">保存</el-button>
          <el-button type="danger" @click="getRebuildScript">重新生成</el-button>
          <el-button type="danger" @click="moduleManerge">模版管理</el-button>
          <el-card class="box-card" style="margin-top: 10px;">
            <div slot="header" class="clearfix">
            </div>
            <div style="height: 500px;">
              <el-input  type="textarea" placeholder="请输入内容" v-model="script.universalInfo">
              </el-input>
            </div>
          </el-card>
        </div>
      </div>
    </div>

    <el-dialog title="模块管理" :visible.sync="addProjectVisible" v-if="addProjectVisible" :show-close="false"
      :close-on-click-modal="false" width="80%" center>
      <el-row>
        <el-col :span="12">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>话术模版</span>
            </div>
            <div class="temp_content" style="height: 500px;">
              <el-input type="textarea" rows="27" placeholder="请输入内容" v-model="script.allTemplate">
              </el-input>
            </div>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>赋分规则</span>
            </div>
            <div class="temp_content" style="height: 500px;">
              <el-input type="textarea" rows="27" placeholder="请输入内容" v-model="script.ruleTemplate">
              </el-input>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <!-- 按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="scrpitTemplateSave">保 存</el-button>
        <el-button @click="projectReset">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAsstntScriptInfoApi, getAsstntScriptTemplateApi, addOrUpdateAsstntScriptInfoApi, addOrUpdateAsstntScriptTemplateApi, getRebuildScriptApi } from "@/api/medicalLibrary/scriptManager";
import constant from "@/utils/constant";

import asstntStndrdItems from '../../../../common/asstntStndrdItems.vue';


export default {
  components: {
    asstntStndrdItems,
  },
  data() {
    return {
      script: {
        universalInfo: "",
        allTemplate: "",
        ruleTemplate: "",
      },
      diseaseRuleDic: [],
      // 点击保存、导入按钮时显示加载中
      importLoading: false,
      healthIndex: '',
      healthRow: {},
      commonProperty: "",//AI话术管理体检项配置id
      // 体检项目库数据
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 分页
      page: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
      },
      currentRow: {},

      // 添加项目弹窗
      addProjectVisible: false,
      projectRules: {
        healthAdvice: [
          { required: true, message: '健康建议', trigger: 'blur' }
        ],
      },
    }
  },
  created() {
  },
  methods: {
    getRebuildScript() {
      if(this.commonProperty == undefined || this.commonProperty =='' || this.commonProperty == null){
          this.$message.closeAll()
            this.$message.warning('请选择体检项目!')
            throw new Error('')
      }
      //重新生成话术
      getRebuildScriptApi({ id: this.commonProperty }).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          this.script.universalInfo = res.data.rebuildScript || ""
          this.$message.success('重新生成成功!')
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    moduleManerge() {
      if(this.commonProperty == undefined || this.commonProperty =='' || this.commonProperty == null){
          this.$message.closeAll()
            this.$message.warning('请选择体检项目!')
            throw new Error('')
      }
      //整体模版
      this.script.allTemplate = ''
      //赋分规则模版
      this.script.ruleTemplate = ''
      //获取体检项目对应的AI话术模版配置信息
      getAsstntScriptTemplateApi({ id: this.commonProperty }).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          var templateArry = res.data
          for (let one of templateArry) {
            if (one.templateType == 1) {
              //整体模版
              this.script.allTemplate = one.universalTemplates
            } else if (one.templateType == 2) {
              //赋分规则模版
              this.script.ruleTemplate = one.universalTemplates
            }
          }
          this.addProjectVisible = true
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    queryStandItemTableData(id, itemsCode, isLock, currentRow) {
      this.commonProperty = itemsCode;
      this.currentRow = currentRow;
      this.page = {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
      }
      this.script.universalInfo = "";
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      //获取体检项目对应的话术内容信息
      getAsstntScriptInfoApi({ id: this.commonProperty }).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          this.script.universalInfo = res.data.universalInfo || ""
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    getListData(itemsCode) {
      getAsstntScriptInfoApi({ itemsCode: itemsCode }).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          tthis.script.universalInfo = res.data.universalInfo
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    // 页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.pageIndex = 1;
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.pageIndex = val;
    },
    // 点击子项目保存接口
    standPropertySaveClick() {
      if(this.commonProperty == undefined || this.commonProperty =='' || this.commonProperty == null){
          this.$message.closeAll()
            this.$message.warning('请选择体检项目!')
            throw new Error('')
        }
      this.saveScriptnfo()
    },
    saveScriptnfo() {
      let params = {
        universalInfo: this.script.universalInfo,
        itemsCode: this.commonProperty,
      }
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      addOrUpdateAsstntScriptInfoApi(params).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          this.$message.closeAll()
          this.$message.success('保存成功')
          this.getListData(this.commonProperty)
          this.$refs.projectLibrary.currentId = null
          this.$refs.projectLibrary.filterText = ''
          this.$refs.projectLibrary.getProjectLibraryList(numbers.indexOf(this.commonProperty))
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    // 弹窗按钮
    // 点击确认
    scrpitTemplateSave() {
      var templateList = [];
      templateList.push({ templateType: 1, universalTemplates: this.script.allTemplate })
      templateList.push({ templateType: 2, universalTemplates: this.script.ruleTemplate })
      let params = {
        ausAsstntScriptTemplates: templateList,
        itemsCode: this.commonProperty,
      }
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      addOrUpdateAsstntScriptTemplateApi(params).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          this.$message.closeAll()
          this.$message.success('保存成功')
          this.addProjectVisible = false;
          this.getListData(this.commonProperty)
          this.$refs.projectLibrary.currentId = null
          this.$refs.projectLibrary.filterText = ''
          this.$refs.projectLibrary.getProjectLibraryList(numbers.indexOf(this.commonProperty))
        }
      })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        })
    },
    // 点击取消
    projectReset() {
      this.addProjectVisible = false
      this.script.allTemplate = ''
      this.script.ruleTemplate = ''

    }
  },
}

</script>

<style lang="less" scoped>
  /deep/.el-textarea__inner{
    height:500px !important;
  }
.handle-btn {
  cursor: pointer;
  color: #409eff;
  display: inline-block;
  padding: 0 8px;
}

.right-title {
  background: #f8f8f8;
  padding: 10px 25px;
  margin-left: 15px;
  position: relative;
}

.right-title::before {
  content: '';
  position: absolute;
  top: 11px;
  left: 15px;
  width: 3px;
  height: 18px;
  background: #f76767;
}

.top_box {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid rgb(241, 238, 238);
  margin-bottom: 15px;
}

.top_box h1 {
  font-weight: 600;
  margin-left: 15px;
}

.left_box {
  width: 400px;
  border: 1px solid #e3dddd;
}

.right_box {
  width: calc(100% - 350px);
  /* border: 1px solid #e3dddd; */
}

/deep/ .filter-tree {
  max-height: calc(100vh - 340px);
  overflow: auto;
}

.temp_content {
  height: 500px;
  overflow-y: auto;
}
</style>